<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_log_master extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.nidn';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$module = isset($_POST['s_module']) ? strval($_POST['s_module']) : 'jenjang';
		$aksi = isset($_POST['s_aksi']) ? strval($_POST['s_aksi']) : 'add';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			
			#===========              query
			switch($module){
				case 'jenjang': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan kode jenjang ', a.kd_jenjang) as deskripsi", false);
									$this->db->from('ref_jenjang a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah kode jenjang ', a.kd_jenjang) as deskripsi", false);
									$this->db->from('ref_jenjang a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus kode jenjang ', a.kd_jenjang) as deskripsi", false);
									$this->db->from('ref_jenjang a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
					
				case 'predikat': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_predikat a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_predikat a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_predikat a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
					
				case 'program': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_program a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_program a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_program a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
					
				case 'fakultas': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.nama_fakultas) as deskripsi", false);
									$this->db->from('ref_fakultas a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.nama_fakultas ) as deskripsi", false);
									$this->db->from('ref_fakultas a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.nama_fakultas) as deskripsi", false);
									$this->db->from('ref_fakultas a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
					
				case 'prodi': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.nama_prodi) as deskripsi", false);
									$this->db->from('ref_prodi a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.nama_prodi ) as deskripsi", false);
									$this->db->from('ref_prodi a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.nama_prodi) as deskripsi", false);
									$this->db->from('ref_prodi a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
					
				case 'jabatan': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.nama) as deskripsi", false);
									$this->db->from('ref_jenisjabatan a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.nama ) as deskripsi", false);
									$this->db->from('ref_jenisjabatan a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.nama) as deskripsi", false);
									$this->db->from('ref_jenisjabatan a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
				case 'pejabat': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_pejabat a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.Nama ) as deskripsi", false);
									$this->db->from('ref_pejabat a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_pejabat a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;	
				case 'dosen': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.nama_dosen) as deskripsi", false);
									$this->db->from('ref_dosen a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.nama_dosen ) as deskripsi", false);
									$this->db->from('ref_dosen a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.nama_dosen) as deskripsi", false);
									$this->db->from('ref_dosen a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
					
				case 'kelompok_mk': if($aksi == 'add'){
									$this->db->select("CDATE as tanggal, b.user_name as username, concat('Menambah record dengan nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_mkkelompok a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
								}else if($aksi == 'update'){
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Mengubah nama ', a.Nama ) as deskripsi", false);
									$this->db->from('ref_mkkelompok a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									$this->db->where('a.MUID is not null');
									$this->db->where('a.status =', 'A');
								}else{
									$this->db->select("MDATE as tanggal, b.user_name as username, concat('Menghapus nama ', a.Nama) as deskripsi", false);
									$this->db->from('ref_mkkelompok a');
									$this->db->join('sys_user b', 'b.user_id = a.CUID');
									//$this->db->where('a.MUID is not null');
									$this->db->where('a.status !=', 'A');
								}
					break;
			}
			
			if($aksi == 'add'){
				$this->db->order_by('CDATE', 'DESC');
			}else{
				$this->db->order_by('MDATE', 'DESC');
			}
			
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
}

?>